package com.sidneysimmons.financially.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.sidneysimmons.financially.Account;
import com.sidneysimmons.financially.R;
import com.sidneysimmons.financially.activities.AccountActivity;

public class NewAccountDialogFragment extends DialogFragment {
	
	/** Used by the FragmentManager to identify this fragment. */
	public static String TAG = "new_account_dialog";
	
	private View newAccountView;

    public static NewAccountDialogFragment newInstance() {
    	NewAccountDialogFragment fragment = new NewAccountDialogFragment();
    	
    	/* // We could dynamically put data into the dialog here
        Bundle bundle = new Bundle();
        bundle.putInt("title", title);
        fragment.setArguments(bundle); */
    	
        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    	// Use this to get dynamically put data, everything is static now
        // int title = getArguments().getInt("title");
    	
    	// Inflate the view to use
    	Context context = getActivity();
    	LayoutInflater inflater = LayoutInflater.from(context);
    	newAccountView = inflater.inflate(R.layout.new_account_dialog, null);
    	
        return new AlertDialog.Builder(context)
        		.setView(newAccountView)
                .setPositiveButton(R.string.new_account_dialog_text_create,
                    new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							// populate Account and send back to UI
							((AccountActivity)getActivity()).validateAndCreateNewAccount(populateAccount(newAccountView));
						}
                    }
                )
                .setNegativeButton(R.string.new_account_dialog_text_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        	// Let the system close the dialog
                        }
                    }
                )
                .create();
    }
    
    /**
     * Populates and returns a new Account object.  Populates from all the fields on the 
     * AlertDialog view.
     * 
     * @param newAccountView - the View to get the data from
     * @return a populated Account object
     */
    protected Account populateAccount(View newAccountView) {
    	Account account = new Account();
    	
    	// Get fields here
    	EditText accountNameField = (EditText) newAccountView.findViewById(R.id.new_account_dialog_edittext_savings_account_name);
    	
    	// Set data here
    	account.setTitle(accountNameField.getText().toString().trim());
    	
    	return account;
    }
}